package ink.onei.web.controller.api;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import ink.onei.common.api.map.AMap;
import ink.onei.common.core.domain.AjaxResult;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.JsonNode;
import kong.unirest.core.Unirest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: nekotako
 * @Description: TODO
 * @Date: 15/02/2024 22:00 Thursday
 */

@RestController
@RequestMapping("/api")
public class AmapController {
    /**
     * @param longitude
     * @param latitude
     * @return
     */
    @RequestMapping("/getAddress")
    public AjaxResult getAddress(String longitude, String latitude) {
        AjaxResult result = AjaxResult.success();
        HttpResponse<String> httpResponse = Unirest.get(AMap.getReoURL(longitude + "," + latitude)).asString();
        httpResponse.ifSuccess(response -> {
            String responseBody = response.getBody();
            JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject().getAsJsonObject("regeocode");
            result.put("addressComponent", jsonObject.getAsJsonObject("addressComponent"));
            result.put("formatted_address", jsonObject.get("formatted_address"));
        });
        httpResponse.ifFailure(response -> {
            result.put("code",404);
            result.put("msg","网络错误");
        });
        return result;
    }
}
